home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Shareware Grab Bag
/
Shareware Grab Bag.iso
/
090
/
cmln1084.arc
/
CPORT1.LTG
< prev
next >
Wrap
Text File
|
1986-10-01
|
2KB
|
112 lines
/* dgt2.c - test the dgsm.c and dgsx.c routines */
#include <stdio.h>
#include "dgsdefs.h"
#define NULL 0
#define MAXLEN 50
#define DEPTH 5
main()
{
FILE *fopen(),*fp;
struct tblctrl *tctrl;
struct xtbctrl *xctrl;
char fname[30],str[MAXLEN],*malloc();
int nsym;
for(;;)
{
fprintf(stdout,"Enter file name: ");
if(fscanf(stdin,"%s",fname) != 1)
fprintf(stderr,"Syntax error on file name entry!");
else if((fp=fopen(fname,"r")) != NULL)
break;
else
fprintf(stderr,"Invalid file name!\n");
}
/* Initialize the control table */
tctrl=galloc();
intdgs(tctrl,DEPTH);
/* Read the tokens into the dgs */
nsym=0;
while(gtstr(fp,str,MAXLEN) != EOF)
{
fprintf(stdout,"%s\n",str);
nsym++;
if(dgs(tctrl,str,nsym) != nsym)
{
fprintf(stderr,"Error on # %3d, %s\n",nsym,str);
exit(1);
}
}
/* "Rewind" the input file */
fclose(fp);
if((fp=fopen(fname,"r")) == NULL)
{
fprintf(stderr,"Unable to reopen input file!\n");
exit(1);
}
nsym=0;
while(gtstr(fp,str,MAXLEN) != EOF)
{
nsym++;
if(srdgs(tctrl,str) != nsym)
fprintf(stdout,"Symbol #%3d not found!\n",nsym);
}
fclose(fp);
fprintf(stdout,"# entered: %3d\n",tctrl->nentry);
fprintf(stdout,"table size in bytes: %d\n",mtrsiz(tctrl));
fprintf(stdout,"# nodes: %3d # links: %3d #chars: %3d\n",
tctrl->nnodes,tctrl->nlinks,tctrl->nchars);
xctrl=xalloc();
if(!cdgx(tctrl,xctrl))
{
fprintf(stderr,"Error in cdgx module!\n");
exit(1);
}
/* print the compressed tree nodes
xprt(xctrl);
*/
if((fp=fopen(fname,"r")) == NULL)
{
fprintf(stderr,"Unable to reopen input file!\n");
exit(1);
}
nsym=0;
while(gtstr(fp,str,MAXLEN) != EOF)
{
nsym++;
if(sgx(xctrl,str) != nsym)
fprintf(stdout,"Symbol #%3d not found in compressed tree!\n",nsym);
}
fclose(fp);
fretre(tctrl);
fprintf(stdout,"Size of compressed tree: %d\n",xctrl->size);
}
/*--- GTSTR -----------------------------------------------------------
Return a non-null line from the requested input device. Any line longer
than 'maxlength-1' is truncated. The length of the line is normally
returned. EOF is returned on eof or error.
-----------------------------------------------------------------------*/
gtstr(fp,str,maxlength)
char *str;
int maxlength;
FILE *fp;
{
int c=0,i=0;
while(i==0 && c!=EOF)
{
while((c=getc(fp))!=EOF && c!='\n' && i<maxlength-1)
{
*str++=c;
i++;
}
}
*str='\0';
return(c==EOF ? EOF : i);
}